primes :: Integral a => [a]
primes = 2 : filter ( (==1) . length . primeFactors ) [3,5..]

primeFactors :: Integral a => a -> [a]
primeFactors n = factor n primes
	where
	factor n (p:ps)
		| p*p > n		 = [n]
		| n `mod` p == 0 = p : factor ( n `div` p )	(p:ps)
		| otherwise		 = factor n ps

isPrime :: Integral a => a -> Bool
isPrime n
	| n <= 1 	= False
	| otherwise =((length(primeFactors n)) == 1)

-- use 1 as a wildcard and replace it with other digits in numbers which have at least 3 1s. 

-- count the number of a specific digit (will be 1 when used below)
numChar :: Char -> Integer -> Int
numChar n m = length $ filter (==n) $ show m


-- returns d if e is equal to c; otherwise it returns e unchanged
replaceIfEq :: Char -> Char -> Char -> Char
replaceIfEq c d e
	| e == c = d
	| otherwise = e

-- takes a number and returns it with all digits matching the first argument with the 2nd
nextNumber :: (Char -> Char) -> Integer -> Integer
nextNumber replaceFunction = read.map replaceFunction.show
	
same n = length $ filter isPrime [x | a<-['1'..'9'], let x = nextNumber (replaceIfEq '1' a) n]

euler_051 = head [ n | n<-[100003,100005..999999],
					3 == numChar '1' n,
					8 == same n]